package com.ssm.user.controller;

import com.ssm.redis.util.CacheUtil;
import com.ssm.redis.util.RedisShareRedLock;
import com.ssm.redis.util.RedisUtil;
import com.ssm.user.entity.po.UserPo;
import com.ssm.user.entity.req.UserReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class TestController {

    @Autowired
    private RedisShareRedLock redisShareRedLock;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private CacheUtil cacheUtil;

    @GetMapping("test")
    public String test() {
        return "hello world";
    }

    @GetMapping()
    public void RedisTest() {
        redisUtil.set("name", "ssm");
    }

    @GetMapping("testRedisLock")
    public void testRedisLock() {
        boolean lock = redisShareRedLock.lock("ssm", "123321", 100000L);
        System.out.println(lock);
    }
    @GetMapping("testLog")
    public void testLog() {
        //集成异步日志前2500ms,集成后60s左右
       long startTome = System.currentTimeMillis();
       for(int i = 0; i < 100000; i ++) {
           log.info("这是第{}条日志", i);
       }
       long endTime = System.currentTimeMillis();
       log.info("当前打印日志耗时：{}", endTime - startTome);
    }

    @GetMapping("testLocalCache")
    public void testLocalCache(@RequestParam List<Long> ids) {

        //获取SkuInfo商品信息

        //()里的为入参，声明入参为list，eg:id-> userService.queryById(id) 我们的方法未使用入参参数list
        cacheUtil.getResult(ids, "skuInfo.skuName", SkuInfo.class, (list) -> {
            Map<Long, SkuInfo> skuInfo = getSkuInfo(ids); //获取数据
            return skuInfo;
        });
    }

    //定义两个方法，模拟获取商品信息和商品价格
    public Map<Long, SkuInfo> getSkuInfo(List<Long> ids) {
        return Collections.emptyMap();
    }
    public Map<Long, SkuPrice> getSkuPrice(List<Long> ids) {
        return Collections.emptyMap();
    }

    //定义两个内部类，商品信息和商品价格
    class SkuInfo {
        private Long id;
        private String name;
    }

    class SkuPrice {
        private Long id;
        private Double price;
    }

    @PostMapping("test/query")
    public void testQuery(@RequestBody UserPo userPo) {
        System.out.println(userPo);
    }
}
